export function isPhoneNumber(tel) {
    var reg = /^0?1[3|4|5|6|7|8][0-9]\d{8}$/;
    return reg.test(tel);
}
//视频判断
export function VideoIndexOf(url) {
    if (url) {
        const images = ['jpg', 'png', 'jpeg']
        const type = url.substring(url.lastIndexOf('.') + 1)
        const isOssType = type.lastIndexOf('?')
        return isOssType !== -1 ? images.indexOf(type.substring(0, type.lastIndexOf('?'))) !== -1 : images.indexOf(type) !== -1
    }
}

//时间转换(js将 “2021-07-06T06:23:57.000+00:00” 转换为年月日时分秒)
export function transformTimestamp(timestamp) {
    let a = new Date(timestamp).getTime();
    const date = new Date(a);
    const Y = date.getFullYear() + '年';
    const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '月';
    const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '日';
    const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
    // const s = date.getSeconds(); // 秒+ h + m;
    const dateString = Y + M + D
    // console.log('dateString', dateString); // > dateString 2021-07-06 14:23
    return dateString;
}
export function myAtoi(s) {
    sign = true
    result = 0
    for (let i in s) {
        if (s[i] == '+') {
            s = s.substring(Number(i) + 1)
            break
        }
        if (s[i] == '-') {
            sign = false
            s = s.substring(Number(i) + 1)
            break
        }
        if (s[i] != ' ') {
            s = s.substring(Number(i))
            break
        }

    }
    for (let i in s) {
        if (!isNaN(s[i])) {
            if (s[i] == ' ') break
            result *= 10
            result += Number(s[i])
        } else break

    }
    if (sign) {
        if (result > (2 ** 31) - 1) {
            return (2 ** 31) - 1
        } else return result
    } else {
        if (result > (2 ** 31)) {
            return (0 - (2 ** 31))
        } else return (0 - result)
    }
}
export function formatSeconds(value) {
    var secondTime = parseInt(value);// 秒
    var minuteTime = 0;// 分
    var hourTime = 0;// 小时
    if (secondTime > 60) {//如果秒数大于60，将秒数转换成整数
        //获取分钟，除以60取整数，得到整数分钟
        minuteTime = parseInt(secondTime / 60);
        //获取秒数，秒数取佘，得到整数秒数
        secondTime = parseInt(secondTime % 60);
        //如果分钟大于60，将分钟转换成小时
        if (minuteTime > 60) {
            //获取小时，获取分钟除以60，得到整数小时
            hourTime = parseInt(minuteTime / 60);
            //获取小时后取佘的分，获取分钟除以60取佘的分
            minuteTime = parseInt(minuteTime % 60);
        }
    }
    var result = "" + parseInt(secondTime) + "秒";

    if (minuteTime > 0) {
        result = "" + parseInt(minuteTime) + "分" + result;
    }
    if (hourTime > 0) {
        result = "" + parseInt(hourTime) + "小时" + result;
    }
    return result;
}


export function formatPhoneNumber(tel) {
    if (!isPhoneNumber(tel)) {
        // console.info('不是手机号')
        return tel;
    }

    return tel.substring(0, 3) + "****" + tel.substr(tel.length - 4)
}
export function stringNum(value) {//判断字符串长度
    var len = value.match(/[^ -~]/g) == null ? value.length : value.length + value.match(/[^ -~]/g).length; 5
    return len

}
export function formatbank(val) {
    return val.substring(0, 3) + "**************" + val.substr(val.length - 4)
}
export function formMonth(val) {
    // return /\d{1,2}-\d{1,2}/g.exec(val)
    return /\d{4}-\d{1,2}-\d{1,2}/g.exec(val)
}
export function Substring(num, str) {
    return str.substring(num, num)
}

// export function subTime(date1) {
//     console.log(getNowTime())
//     var s1 = new Date(date1).getTime(), s2 = new Date(getNowTime()).getTime();
//     return (s1 - s2) / 1000 / 60 / 60
//     // return /\d{4}-\d{1,2}-\d{1,2}/g.exec(time)[0] 年月日
// }
export function subTime(date1) {//将时间戳转为时间
    // console.log(getNowTime())
    var s1 = new Date(date1).getTime(), s2 = new Date(getNowTime()).getTime();
    return new Date(date1) / 1000 / 60 / 60
    // return /\d{4}-\d{1,2}-\d{1,2}/g.exec(time)[0] 年月日
}
export function getTimer() {
    var time = new Date();
    var h = time.getHours();
    h = h < 10 ? '0' + h : h;
    var m = time.getMinutes();
    m = m < 10 ? '0' + m : m;
    var s = time.getSeconds();
    s = s < 10 ? '0' + s : s;
    return h + ':' + m + ':' + s;
}
function getNowTime() {
    let now = new Date();
    let year = now.getFullYear(); //获取完整的年份(4位,1970-????)
    let month = now.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    let today = now.getDate(); //获取当前日(1-31)
    let hour = now.getHours(); //获取当前小时数(0-23)
    let minute = now.getMinutes(); //获取当前分钟数(0-59)
    let second = now.getSeconds(); //获取当前秒数(0-59)
    let nowTime = ''
    nowTime = year + '-' + fillZero(month) + '-' + fillZero(today) + ' ' + fillZero(hour) + ':' +
        fillZero(minute) + ':' + fillZero(second)
    return nowTime.toString().replace(/\-/g, '/')
};
function fillZero(str) {
    var realNum;
    if (str < 10) {
        realNum = '0' + str;
    } else {
        realNum = str;
    }
    return realNum;
}
export function type(type) {
    // var ua = navigator.userAgent.toLowerCase();
    // var pageSource = localStorage.getItem("pageSource");
    //当在微信浏览器的时候
    switch (type) {
        case 0:
            return '公告';
            break;
        case 1:
            return '新品';
            break;

        case 2:
            return "赋能";
            break;
        case 3://app
            return "合成"
            break;
        case 4:
            return "活动"
            break
        default:
            return false;
    }
}
export function divideEqually(originArr) {
    /*此函数用于，把数组平均分成4份。
    * originArr，带分割的数组。*/
    if (originArr.length >= 4) {
        let arr1, arr2, arr3, arr4;
        arr1 = originArr.slice(0, Math.round(originArr.length / 4 * 3));
        arr2 = originArr.slice(Math.round(originArr.length / 4), Math.round(originArr.length / 4) * 2);
        arr3 = originArr.slice(Math.round(originArr.length / 4) * 2, Math.round(originArr.length / 4) * 3);
        arr4 = originArr.slice(Math.round(originArr.length / 4) * 4);

        // console.log(arr1, "arr")
        // console.log(arr2, "arr")
        // console.log(arr3, "arr")
        // console.log(arr4, "arr")
        return [arr1, arr4];
    }
}
export function compressImg(file) {//压缩
    var files
    var fileSize = parseFloat(parseInt(file['size']) / 1024 / 1024).toFixed(2)
    var read = new FileReader()
    read.readAsDataURL(file)
    return new Promise(function (resolve, reject) {
        read.onload = function (e) {
            var img = new Image()
            img.src = e.target.result
            img.onload = function () {
                // 默认按比例压缩
                var w = this.width
                var h = this.height
                // 生成canvas
                var canvas = document.createElement('canvas')
                var ctx = canvas.getContext('2d')
                var base64
                // 创建属性节点
                canvas.setAttribute('width', w)
                canvas.setAttribute('height', h)
                ctx.drawImage(this, 0, 0, w, h)
                if (fileSize < 1) {
                    // 如果图片小于一兆 那么不执行压缩操作
                    base64 = canvas.toDataURL(file['type'], 1)
                } else if (fileSize > 1 && fileSize < 2) {
                    // 如果图片大于1M并且小于2M 那么压缩0.5
                    base64 = canvas.toDataURL(file['type'], 0.5)
                } else {
                    // 如果图片超过2m 那么压缩0.2
                    base64 = canvas.toDataURL(file['type'], 0.2)
                }
                // 回调函数返回file的值（将base64编码转成file）
                resolve(base64)
            }
        }
    })
}
//倒计时
export function countTime(oldtime) {
    let old = oldtime.toString().replace(/\-/g, '/')
    let time = new Date(getNowTime()).getTime(); //当前时间

    let endtime = new Date(old).getTime(); //到期时间

    let surplus = endtime - time;  //剩余时间
    console.log(surplus / 1000 / 60 / 60)
    return (surplus / 1000 / 60 / 60)

    // return surplus.replace(/-/g, '/')
}

//百分数
export function toPercent(point) {
    var percent = Number(point * 100);//.toFixed(1)
    percent += "%";
    return percent;
}
export function FixedPercent(point) {
    var percent = Number(point * 100).toFixed(2)
    percent += "%";
    return percent;
}
//获取比例

export function scale(str, num) {

    if (str) {
        var index = str.indexOf(":");
        let result = str.substr(index + 1, str.length);  //获取-后边的字符串
        let before = str.substring(0, index);  //获取-前边的字符串

        if (num == 0) {
            return result
        } else {

            return before
        }

    } else {
        return 0
    }
}




